package com.shuorikeji.huohuo.ui.sj;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.imageview.ShapeableImageView;
import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.adapter.SdXqMhAdapter;
import com.shuorikeji.huohuo.base.BasePresenterActivity;
import com.shuorikeji.huohuo.bean.MhSdInfoBean;
import com.shuorikeji.huohuo.bean.MhSdXqBean;
import com.shuorikeji.huohuo.bean.MhSjSSubBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.mvp.AllPresenter;
import com.shuorikeji.huohuo.utils.BusinessUtils;
import com.shuorikeji.huohuo.utils.SpacesItemDecoration;
import com.shuorikeji.huohuo.utils.ToastShowUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

public class MhSdDetailActivity extends BasePresenterActivity<AllPresenter> {
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_sd_title)
    TextView tvSdTitle;
    @BindView(R.id.tv_xg)
    TextView tvXg;
    @BindView(R.id.tv_add_mh)
    TextView tvAddMh;
    @BindView(R.id.siv_fm)
    ShapeableImageView sivFm;
    @BindView(R.id.siv_cjr_tx)
    ShapeableImageView sivCjrTx;
    @BindView(R.id.tv_cjr)
    TextView tvCjr;
    @BindView(R.id.tv_jj)
    TextView tvMhJj;
    @BindView(R.id.tv_tags)
    TextView tvTags;
    @BindView(R.id.tv_mhs)
    TextView tvMhs;
    @BindView(R.id.tv_sc)
    TextView tvScs;
    @BindView(R.id.tv_dz)
    TextView tvDzs;
    @BindView(R.id.tv_pls)
    TextView tvPls;
    @BindView(R.id.rv_data)
    RecyclerView rvData;

    private int SD_ID;
    private MhSdInfoBean sdXqInfo;
    private List<MhSjSSubBean> sdMhList = new ArrayList<>();
    private SdXqMhAdapter sdXqMhAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getmImmersionBar().reset().statusBarColor(R.color.transparent).statusBarDarkFont(true, 0.2f).init();
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_mh_sd_detail);
    }

    @Override
    protected void initData() {
        initAdapter();
        initIntent();
        initClick();
    }

    private void initAdapter() {
        rvData.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        rvData.addItemDecoration(new SpacesItemDecoration(0, 0, 15, 0));
        sdXqMhAdapter = new SdXqMhAdapter(mContext, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        MhSjSSubBean data = (MhSjSSubBean) object;
                        delMh(data.getId());
                        break;
                }
            }
        });
        rvData.setAdapter(sdXqMhAdapter);
    }

    private void initIntent() {
        Intent intent = getIntent();
        SD_ID = intent.getIntExtra("SD_ID", -1);
        getMhSdDetail();
    }

    private void getMhSdDetail() {
        mPresenter.mhSdDetail(true, SD_ID, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        MhSdXqBean data = (MhSdXqBean) object;
                        sdXqInfo = data.getData();
                        if (sdXqInfo != null) {
                            showInfo();
                        }
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhSdDetailActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void showInfo() {
        tvSdTitle.setText(sdXqInfo.getTitle());
        BusinessUtils.setLoadImg(this, sdXqInfo.getImg(), sivFm);
        if (sdXqInfo.getUser() != null) {
            BusinessUtils.setLoadImg(this, sdXqInfo.getUser().getAvatar(), sivCjrTx);
            tvCjr.setText(sdXqInfo.getUser().getNickname());
        }
        tvMhJj.setText(sdXqInfo.getContent());
        tvTags.setText(sdXqInfo.getTags());
        tvMhs.setText("共" + sdXqInfo.getInfo().getTotal() + "本");
        tvScs.setText(String.valueOf(sdXqInfo.getCollect_num()));
        tvDzs.setText(String.valueOf(sdXqInfo.getLike_num()));

        sdMhList.clear();
        sdMhList.addAll(sdXqInfo.getInfo().getData());
        sdXqMhAdapter.setData(sdMhList);
    }

    private void delMh(int id) {
        mPresenter.delSdMh(true, id, new ClickCallBack() {
            @Override
            public void onClick(int flag, Object object) {
                switch (flag) {
                    case 1:
                        getMhSdDetail();
                        break;
                    case 2:
                        String msg = (String) object;
                        ToastShowUtil.showToastCenter(MhSdDetailActivity.this, msg);
                        break;
                }
            }
        });
    }

    private void initClick() {
        ivBack.setOnClickListener(view -> {
            finish();
        });
        tvAddMh.setOnClickListener(view -> {
            Intent intent = new Intent(MhSdDetailActivity.this, AddMhActivity.class);
            intent.putExtra("SD_ID", SD_ID);
            startActivityForResult(intent, 1001);
        });
        tvXg.setOnClickListener(view -> {
            if (sdMhList != null && sdMhList.size() > 0) {
                if ("编辑".equals(tvXg.getText().toString())) {
                    tvXg.setText("完成");
                    for (int i = 0; i < sdMhList.size(); i++) {
                        sdMhList.get(i).setDel(true);
                    }
                    sdXqMhAdapter.setData(sdMhList);
                } else if ("完成".equals(tvXg.getText().toString())) {
                    tvXg.setText("编辑");
                    for (int i = 0; i < sdMhList.size(); i++) {
                        sdMhList.get(i).setDel(false);
                    }
                    sdXqMhAdapter.setData(sdMhList);
                }
            } else {
                ToastShowUtil.showToastCenter(MhSdDetailActivity.this, "该书单暂无漫画");
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 1001:
                    getMhSdDetail();
                    break;
            }
        }
    }
}